<template>
  <el-dialog :title="monthDetailTitle" :visible.sync="visible" :fullscreen="true" :close-on-click-modal="false"
    :close-on-press-escape="false" append-to-body>
    <div>
      <el-form :model="monthQueryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
        label-width="120px">
        <el-form-item label="操作时间" prop="dateArray">
          <el-date-picker clearable type="daterange" format="yyyy-MM-dd" v-model="monthQueryParams.dateArray"
            value-format="yyyy-MM-dd HH:mm:ss" :default-time="['00:00:00', '23:59:59']" placeholder="请选择签约日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="学生姓名" prop="stuName">
          <el-input v-model="monthQueryParams.stuName" placeholder="请输入学生姓名" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="学生联系电话" prop="stuTel">
          <el-input v-model="monthQueryParams.stuTel" placeholder="请输入学生联系电话" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="订单状态" prop="validFlag">
          <el-select v-model="monthQueryParams.validFlag" placeholder="请选择订单状态" clearable>
            <el-option v-for="dict in dict.type.order_valid_flag" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="monthDetailData">
        <el-table-column label="学生" align="center" prop="stuName" />
        <el-table-column label="付款" align="center" prop="payState">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.common_state" :value="scope.row.payState" />
          </template>
        </el-table-column>
        <el-table-column label="费用类型" align="center" prop="renewType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.stu_member_type" :value="scope.row.renewType" />
          </template>
        </el-table-column>
        <el-table-column label="订单类型" align="center" prop="orderType">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.orderType==1" type="info">新建</el-tag>
            <el-tag v-if="scope.row.orderType==2" type="warning">续费</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="计算方式" align="center" prop="feeType">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.feeType==1" type="info">费率</el-tag>
            <el-tag v-if="scope.row.feeType==2" type="warning">固定金额</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="订单金额(元)" align="center" prop="orderAmount" />
        <el-table-column label="会员有效期-开始" align="center" prop="startDate" />
        <el-table-column label="会员有效期-结束" align="center" prop="expireDate" />
        <el-table-column label="操作时间" align="center" prop="gmtCreate" />
        <el-table-column label="订单状态" align="center" prop="validFlag">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.order_valid_flag" :value="scope.row.validFlag" />
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="200" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <div v-if="scope.row.payState==1 && scope.row.validFlag!=4">
              <el-button size="mini" type="text" v-if="scope.row.payState==1" icon="el-icon-edit"
                @click="handleUpdate(scope.row,'update')" v-hasPermi="['school:studentorder:edit']">修改</el-button>
              <el-button size="mini" type="text" v-if="scope.row.payState==1" icon="el-icon-delete"
                @click="handleDelete(scope.row)" v-hasPermi="['school:studentorder:abandonment']">废弃</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="monthDetailTotal>0" :total="monthDetailTotal" :page.sync="monthQueryParams.pageNum"
        :limit.sync="monthQueryParams.pageSize" @pagination="getMonthDetailData" />
      <el-dialog title="修改订单" :visible.sync="open" width="600px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="续费期限" prop="renewType">
            <el-radio-group v-model="form.renewType">
              <el-radio-button v-for="dict in dict.type.stu_member_type" :key="dict.value" :label="dict.value"
                border>{{dict.label}}</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm" @keyup.enter.native="submitForm"
            :loading="submitLoading">{{ submitLoading ? '提交中...' : '确定' }}</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </el-dialog>
</template>

<script>
  import {
    listStudentorder,
    getStudentorder,
    abandonment,
    updateStudentorder
  } from "@/api/bus_web/school/studentorder";

  export default {
    name: "order_month_detail",
    dicts: ['stu_member_type', 'common_state', 'order_valid_flag'],
    components: {

    },
    data() {
      return {
        // 遮罩层
        loading: true,
        monthDetailTitle: '',
        // 显示搜索条件
        showSearch: true,
        visible: false,
        // 是否显示弹出层
        open: false,
        submitLoading: false,
        // 总条数
        monthDetailTotal: 0,
        monthDetailData: [],
        monthRow: {},
        monthQueryParams: {
          pageNum: 1,
          pageSize: 10,
          validFlag:null,
          payChargeId:null,
          schoolIdArray: []
        },
        form: {
          id: null,
          stuId: null,
          orderType: null,
          feeType: null,
          renewType: null,
          orderAmount: null,
          schoolId: null,
          startDate: null, // 👈 显式声明
          expireDate: null,
          gmtCreate: null,
          deleted: null,
          gmtModify: null,
          createBy: null,
          updateBy: null
        },
        rules: {}
      };
    },
    methods: {
      init(row) {
        this.monthRow = row
        this.monthDetailTitle = '[' + row.schoolName + ']学生列表'
        this.resetForm("queryForm")
        this.visible = true
        this.getMonthDetailData()
      },
      getMonthDetailData() {
        this.loading = true
        this.monthQueryParams.schoolIdArray = []
        this.monthQueryParams.schoolIdArray.push(this.monthRow.id)
        this.monthQueryParams.payChargeId = this.monthRow.billId
        listStudentorder(this.monthQueryParams).then(response => {
          this.monthDetailData = response.rows;
          this.monthDetailTotal = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.monthQueryParams.pageNum = 1;
        this.getMonthDetailData();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getStudentorder(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改学生续费订单";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.submitLoading = true
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateStudentorder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getMonthDetailData();
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            this.submitLoading = false
          }
        });
      },
      /** 废弃按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认废弃订单编号为"' + ids + '"的数据项？').then(function() {
          return abandonment(ids);
        }).then(() => {
          this.getMonthDetailData();
          this.$modal.msgSuccess("废弃成功");
        }).catch(() => {});
      }, // 表单重置
      reset() {
        this.form = {
          id: null,
          stuId: null,
          orderType: null,
          feeType: null,
          renewType: null,
          orderAmount: null,
          schoolId: null,
          startDate: null,
          expireDate: null,
          gmtCreate: null,
          deleted: null,
          gmtModify: null,
          createBy: null,
          updateBy: null
        };
        this.resetForm("form");
      }, // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
    }
  };
</script>
